package com.yeyuanqi.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vdurmont.emoji.EmojiParser;
import com.yeyuanqi.dao.ArticleMapper;
import com.yeyuanqi.dao.CommentMapper;
import com.yeyuanqi.dao.StatisticMapper;
import com.yeyuanqi.model.domain.Article;
import com.yeyuanqi.model.domain.Statistic;
import com.yeyuanqi.service.IArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private StatisticMapper statisticMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public PageInfo<Article> selectArticleWithPage(Integer page, Integer count) {
        PageHelper.startPage(page,count);
        List<Article> articleList = articleMapper.selectArticleWithPage();
        // 封装文章统计数据
        for (int i = 0; i < articleList.size(); i++) {
            Article article = articleList.get(i);
            Statistic statistic = statisticMapper.selectStatisticWithArticleId(article.getId());
            article.setHits(statistic.getHits());
            article.setCommentsNum(statistic.getCommentsNum());
        }
        PageInfo<Article> pageInfo = new PageInfo<>(articleList);
        return pageInfo;
    }

    @Override
    public List<Article> getHeatArticles() {
        List<Statistic> list = statisticMapper.getStatistic();
        List<Article> articlelist=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Article article = articleMapper.selectArticleWithId(list.get(i).getArticleId());
            article.setHits(list.get(i).getHits());
            article.setCommentsNum(list.get(i).getCommentsNum());
            articlelist.add(article);
            if(i>=9){
                break;
            }
        }
        return articlelist;
    }

    //根据id查询单个文章详情，使用redis缓存管理
    @Override
    public Article selectArticleWithId(Integer id) {
        Article article = null;
        Object o = redisTemplate.opsForValue().get("article_"+id);
        if (o!=null){
            article=(Article) o;
        }else {
            article= articleMapper.selectArticleWithId(id);
            if (article!=null){
                redisTemplate.opsForValue().set("article_"+id,article);
            }
        }
        return article;
    }

    //发布文章
    @Override
    public void publish(Article article) {
        //去除表情
        article.setContent(EmojiParser.parseToAliases(article.getContent()));
        article.setCreated(new Date());
        article.setHits(0);
        article.setCommentsNum(0);
        articleMapper.publishArticle(article);
        statisticMapper.addStatistic(article);
    }

    //更新文章
    @Override
    public void updateArticleWithId(Article article) {
        article.setModified(new Date());
        articleMapper.updateArticleWithId(article);
        redisTemplate.delete("article_"+article.getId());
    }

    //删除文章
    @Override
    public void deleteArticleWithId(int id) {
        articleMapper.deleteArticleWithId(id);
        //删除缓存
        redisTemplate.delete("article_"+id);
        statisticMapper.deleteStatisticWithId(id);
        commentMapper.deleteCommentWithId(id);

    }


    @Override
    public void deleteTagsWithId(int id) {
        articleMapper.deleteTagsWithId(id);
    }

    @Override
    public PageInfo<Article> selectArticleAll(Integer page, Integer count) {
        PageHelper.startPage(page,count);
        List<Article> articleList2 = articleMapper.selectArticleAll();
        PageInfo<Article> pageInfo = new PageInfo<>(articleList2);
        return pageInfo;
    }
}
